const app = getApp();
const baseUrl = app.globalData.baseUrl,
	common = require('../../../utils/common');
let Did = "";
Page({
	data: {
		StatusBar: app.globalData.StatusBar,
		CustomBar: app.globalData.CustomBar,
		// 说说内容主体
		comment: [],
		// 回复主体
		reply: [],
		// 分页的第一页
		pageindex: 1,
		// 是否还有新消息， 也就是和分页搭配使用的
		haveNew: true,
		// 切换转发评论赞
		module: 1,
		// 从前一个页面进入看到的两种方式
		showcomment: true,
		// 点赞的人
		likepeople: [],
		// 评论功能的显示与否
		isComment: false,
		func: false, //触发评论的方式false是回复按钮，true是评论按钮
		commentComtent: "", //发布的内容
		commentPic: [], //发布的内图片数组
		dcid: '' //点击评论按钮时候的

	},
	onLoad: function (options) {
		// 获取url带来的参数
		// console.log(options);
		const Uid = app.globalData.loginInfo.Uid;
		Did = options.did;
		let type = options.type;
		if (type == 1) {
			this.setData({
				showcomment: false
			})
		}
		let pageindex = this.data.pageindex;
		let that = this;
		wx.request({
			url: baseUrl + '/api/dynamic/getdynamicbydid',
			data: {
				Uid,
				Did
			},
			method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				// console.log(res);
				if (res.data.code == 200) {
					that.setData({
						comment: res.data.data
					});
				}
				if (res.data.data.length < 5) {
					that.setData({
						haveNew: false
					})
				}
				// success
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		})

		wx.request({
			url: baseUrl + '/api/dynamic/getmaincomment',
			data: {
				Uid,
				Did,
				pageindex: pageindex
			},
			method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				if (res.data.code == 200) {
					that.setData({
						reply: res.data.data
					})
				}
				// success
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		})
	},
	likeAction(e) {
		const Uid = app.globalData.loginInfo.Uid;
		const DCid = e.currentTarget.dataset.did;
		const type = 1;
		let changeLike = "comment.isLike";
		let state = !this.data.comment.isLike;
		if (state) {
			wx.request({
				url: baseUrl + '/api/dynamic/setlike',
				data: {
					DCid,
					Uid,
					type
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function (res) {
					// success
				},
				fail: function () {
					// fail
				},
				complete: function () {
					// complete
				}
			});
		} else {
			wx.request({
				url: baseUrl + '/api/dynamic/cancellike',
				data: {
					DCid,
					Uid,
					type
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function (res) {
					// console.log(res);
					// success
				},
				fail: function () {
					// fail
				},
				complete: function () {
					// complete
				}
			});
		}
		this.setData({
			[changeLike]: state
		});
	},
	tw_likeAction(e) {
		const Uid = app.globalData.loginInfo.Uid;
		const DCid = e.currentTarget.dataset.did;
		const type = 2;
		let needindex = common.searchMem(this.data.reply, 'DCid', DCid);
		let changeLike = "reply[" + needindex + "].islike",
			changeNum = "reply[" + needindex + "].likenum";
		let state = !this.data.reply[needindex].islike,
			num = this.data.reply[needindex].likenum;
		// console.log(num);
		if (state) {
			num++;
			wx.request({
				url: baseUrl + '/api/dynamic/setlike',
				data: {
					DCid,
					Uid,
					type
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function (res) {
					// console.log(res);
					// success
				},
				fail: function () {
					// fail
				},
				complete: function () {
					// complete
				}
			});
		} else {
			num--;
			wx.request({
				url: baseUrl + '/api/dynamic/cancellike',
				data: {
					DCid,
					Uid,
					type
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function (res) {
					// console.log(res);
					// success
				},
				fail: function () {
					// fail
				},
				complete: function () {
					// complete
				}
			});
		}
		this.setData({
			[changeLike]: state,
			[changeNum]: num
		});
	},
	collectAction(e) {
		const Uid = app.globalData.loginInfo.Uid;
		const Did = e.currentTarget.dataset.did;
		let changeCollection = "comment.isCollection";
		let state = !this.data.comment.isCollection;
		if (state) {
			wx.request({
				url: baseUrl + '/api/dynamic/setcollection',
				data: {
					Did,
					Uid
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function (res) {
					// console.log(res);
					// success
				},
				fail: function () {
					// fail
				},
				complete: function () {
					// complete
				}
			});
		} else {
			wx.request({
				url: baseUrl + '/api/dynamic/cancelcollection',
				data: {
					Did,
					Uid
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function (res) {
					// console.log(res);
					// success
				},
				fail: function () {
					// fail
				},
				complete: function () {
					// complete
				}
			});
		}
		this.setData({
			[changeCollection]: state
		});

	},
	onReachBottom() {
		const Uid = app.globalData.loginInfo.Uid;
		let pageindex = this.data.pageindex + 1;
		this.setData({
			pageindex: pageindex
		});
		let that = this;
		if (that.data.haveNew) {
			wx.request({
				url: baseUrl + '/api/dynamic/getmaincomment',
				data: {
					Uid,
					Did,
					pageindex: pageindex
				},
				method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function (res) {
					if (res.data.code == 200) {
						that.setData({
							reply: that.data.reply.concat(res.data.data)
						})
					}
					if (res.data.data.length < 5) {
						that.setData({
							haveNew: false
						})
					}
					// success
				},
				fail: function () {
					// fail
				},
				complete: function () {
					// complete
				}
			})
		}

	},
	changeindex(e) {
		let that = this;
		this.setData({
			module: e.target.dataset.idx
		})
		wx.request({
			url: baseUrl + '/api/dynamic/getlikeuser',
			data: {
				Did
			},
			method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				if (res.data.code == 200) {
					that.setData({
						likepeople: res.data.data
					})
				}
				// success
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		})
	},
	onPageScroll: function (e) {
		if (e.scrollTop < 10) {
			this.setData({
				showcomment: true
			})
		}
	},
	callback(e) {
		console.log(e);
		// 这里需要修改很多逻辑
	},
	moveto: function (e) {
		const Uid = app.globalData.loginInfo.Uid;
		const DCid = e.currentTarget.dataset.did;
		// console.log(Uid, DCid,e);
		wx.navigateTo({
			//  url: 'test?id=1' 可以带参数 dataset里面的只有小写
			url: '../reply_reply/reply_reply?did=' + DCid + '&Uid=' + Uid,
			success: function (res) {
				// success
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		})
	},
	callback() {
		common.goBack();
	},
	tw_sendreply(e) {
		let stat = false;
		if ( !e.currentTarget.dataset.dcid) {
			stat = true
		}
		this.setData({
			isComment: true,
			func: stat,
			dcid: e.currentTarget.dataset.dcid
		})
	},
	inputFocus(e) {
		console.log(e, '键盘弹起');
		// var inputHeight = 0;
		// if (e.detail.height) {
		// 	inputHeight = e.detail.height;
		// }
		// this.setData({
		// 	textheight: inputHeight,
		// 	pos: 'static'
		// });
	},
	cancelMask() {
		this.setData({
			isComment: false
		})
	},
	bindTextAreaBlur: function (e) {
		this.setData({
			commentComtent: e.detail.value
		})
		// console.log(this.data.commentComtent)
	},
	donone(e) {
	},
	//添加发布的图片
	chooseImage(e) {
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success: res => {
				let images = this.data.commentPic.concat(res.tempFilePaths);
				let imagesArr = images.length <= 1 ? images : images.slice(0, 1);
				this.setData({
					commentPic: imagesArr,
				});
			}
		})
	},
	//删除已经选择的图片
	deletepic(e) {
		const id = e.currentTarget.dataset.bindex;
		this.data.commentPic.splice(id, 1);
		this.setData({
			commentPic: this.data.commentPic
		});
	},
	submitForm(e) {
		let content = this.data.commentComtent,
			Uid = app.globalData.loginInfo.Uid,
			isreply = this.data.func,
			that = this,
			arr = this.data.commentPic,
			piclenth = arr.length,
			selectPic = [];
			
		let getfile = (i) => {
			const pro = new Promise(function (resolve, reject) {
				wx.uploadFile({
					url: baseUrl + '/api/user/uploadimage',
					filePath: i,
					name: 'image',
					formData: {
						Uidentity: Uid
					},
					success(res) {
						var json = JSON.parse(res.data);
						selectPic.push(json.data);
						selectPic = selectPic.length <= 1 ? selectPic : selectPic.slice(0, 1)
						resolve(res);
					},
					error(res) {
						reject(res);
					}
				})
			})
			return pro;
		}
		if (piclenth || content) {
			let promises = arr.map(function (i) {
				return getfile(i);
			});
			Promise.all(promises).then(function () {
				if (isreply) {
					wx.request({
						url: baseUrl + '/api/dynamic/pubcomment',
						data: {
							Did,
							isreply,
							image: selectPic[0] || '',
							Uid: Uid,
							cont: content,
						},
						header: {
							'content-type': 'application/json' // 默认值
						},
						method: 'POST',
						success(res) {
							let temval = that.data.reply;
							temval.unshift(res.data.data);
							that.setData({
								reply: temval
							});
							that.cancelMask();
							console.log(that.data.reply)
							// 这个时候，只需要在that.data.reply加一个对象就行
						}
					})
				} else {
					let DCid, toDCid;
					DCid = that.data.dcid;
					toDCid = that.data.dcid;
					wx.request({
						url: baseUrl + '/api/dynamic/pubcomment',
						data: {
							Did,
							isreply,
							image: selectPic[0] || '',
							Uid: Uid,
							cont: content,
							DCid,
							toDCid
						},
						header: {
							'content-type': 'application/json' // 默认值
						},
						method: 'POST',
						success(res) {
							let temindex = common.searchMem(that.data.reply,'DCid',DCid);
							let temval = that.data.reply[temindex].childarr;
							console.log(that.data.reply)
							temval.unshift(res.data.data);
							let temkey = 'reply[' + temindex + '].childarr'
							that.setData({
								[temkey] : temval
							})
							that.cancelMask();
							console.log(that.data.reply)
							// 这个时候，需要先找到是哪一个that。data.reply需要children
							// console.log(common.searchMem(that.data.reply,'DCid',DCid));
						}
					})
				}
				that.setData({
					commentPic :[]
				})
			}).catch(function (e) {
				console.log(e)
			})
		}

	},
});